package com.user.profile.service.label;

import com.user.profile.enums.OperateEnum;
import com.user.profile.perm.ClickHousePerm;
import com.user.profile.request.SaveLabelRequest;
import com.user.profile.request.SaveLabelRequest.UserInfo;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javafx.util.Pair;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author linlx
 * @Date 2021/9/18 9:34
 * @Description
 **/
@Service
public class UserAttributeCkBuildImpl extends AbstractCkBuildService {

  @Resource
  private CommonHelper commonHelper;

  @Override
  public Pair<StringBuilder, Map<Integer, UserInfo>> buildAllOperate(SaveLabelRequest request) {
    // 拼接主子标签的关联关系
    return commonHelper
        .buildAllOperate(request.getUserAttributesData());
  }

  @Override
  public String buildCkSQL(Pair<StringBuilder, Map<Integer, UserInfo>> pair,
      Integer parentOperate) {
    // 没有用户属性标签
    if (null == pair) {
      return StringUtils.EMPTY;
    }
    StringBuilder operateBuilder = pair.getKey();
    Map<Integer, UserInfo> allMap = pair.getValue();
    // 单标签
    if (null == parentOperate) {
      operateBuilder = new StringBuilder("bitmapToArray(").append(operateBuilder).append(")");
    }
    StringBuilder result = new StringBuilder("SELECT ");
    result.append(operateBuilder.toString()).append(" AS user_number1, 1 AS AID ");
    Set<Entry<Integer, UserInfo>> sonEntries = allMap.entrySet();
    int setIndex = 0;
    for (Entry<Integer, UserInfo> entry : sonEntries) {
      StringBuilder ckSql = new StringBuilder(ClickHousePerm.SELECT_SON);
      if (setIndex == 0) {
        result.append("FROM (");
      } else {
        result.append("INNER JOIN (");
      }
      // 拼接子句
      ckSql.append("user").append(entry.getKey()).append(" FROM");
      UserInfo attributesInfo = entry.getValue();
      if ("VIP等级".equals(attributesInfo.getKey())) {
        this.buildLabelString(ckSql, attributesInfo, result, setIndex);
      } else if ("年龄".equals(attributesInfo.getKey())) {
        this.buildLabelInt(ckSql, attributesInfo, result, setIndex);
      }
      if (setIndex != 0) {
        // 拼接关联关系
        result.append(" ON ").append("a").append(setIndex - 1).append(".").append("join_id")
            .append("=")
            .append("a").append(setIndex).append(".").append("join_id ");
      }
      setIndex++;
    }
    System.out.println(result.toString());
    return result.toString();
  }

  private void buildLabelInt(StringBuilder ckSql, UserInfo attributesInfo,
      StringBuilder result, int setIndex) {
    ckSql.append(" ch_label_int where (");
    ckSql.append("labelname = ").append("'").append(attributesInfo.getKey()).append("'")
        .append(" and ")
        .append("labelvalue ").append(OperateEnum.get(attributesInfo.getOperate()).getName())
        .append(attributesInfo.getValue())
        .append(")");
    result.append(ckSql.toString());
    result.append(") ").append("a").append(setIndex).append(" ");
  }

  private void buildLabelString(StringBuilder ckSql, UserInfo attributesInfo,
      StringBuilder result, int setIndex) {
    ckSql.append(" ch_label_string where (");
    ckSql.append("labelname = ").append("'").append(attributesInfo.getKey()).append("'")
        .append(" and ")
        .append("labelvalue ").append(OperateEnum.get(attributesInfo.getOperate()).getName())
        .append("'")
        .append(attributesInfo.getValue())
        .append("'")
        .append(")");
    result.append(ckSql.toString());
    result.append(") ").append("a").append(setIndex).append(" ");
  }
}
